package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.service.ShoppingCartService;
import net.bytebuddy.matcher.EqualityMatcher;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    @Transactional
    public void add(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //先判断当前购物车是否存在当前添加菜品或套餐
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId())
                .eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId())
                .eq(shoppingCart.getDishFlavor() != null, ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor())
                .eq(shoppingCart.getUserId() != null, ShoppingCart::getUserId, shoppingCart.getUserId());

        ShoppingCart cart = this.getOne(queryWrapper);
        //如果购物车中存在该添加的菜品或者套餐,对应的数量+1即可
        if (cart != null) {
            cart.setNumber(cart.getNumber() + 1);
            LambdaUpdateWrapper<ShoppingCart> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            //找到购物车的唯一主键进行添加，而不是用户id，因为一个用户的购物车可能有多个菜品或者套餐
            lambdaUpdateWrapper.eq(cart.getId() != null, ShoppingCart::getId, cart.getId());
            this.update(cart, lambdaUpdateWrapper);
        } else {
            //如果购物车中不存在该添加的菜品或者套餐,将该菜品或者套餐加入购物车中
            //判断当前添加的是菜品还是套餐
            Long dishId = shoppingCart.getDishId();
            if (dishId != null) {
                Dish dish = dishService.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            } else {
                Setmeal setmeal = setmealService.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            this.save(shoppingCart);
        }

    }

    @Override
    public void del(ShoppingCartDTO shoppingCartDTO) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(shoppingCartDTO.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCartDTO.getSetmealId())
                .eq(shoppingCartDTO.getDishId()!=null,ShoppingCart::getDishId,shoppingCartDTO.getDishId())
                .eq(shoppingCartDTO.getDishFlavor()!=null,ShoppingCart::getDishFlavor,shoppingCartDTO.getDishFlavor());
        ShoppingCart shoppingCart = this.getOne(queryWrapper);
        Integer number = shoppingCart.getNumber();
        if(number==1){
            this.remove(queryWrapper);
        }else{
            shoppingCart.setNumber(number-1);
            LambdaUpdateWrapper<ShoppingCart> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(shoppingCartDTO.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCartDTO.getSetmealId())
                    .eq(shoppingCartDTO.getDishId()!=null,ShoppingCart::getDishId,shoppingCartDTO.getDishId())
                    .eq(shoppingCartDTO.getDishFlavor()!=null,ShoppingCart::getDishFlavor,shoppingCartDTO.getDishFlavor());
            this.update(shoppingCart,lambdaUpdateWrapper);
        }
    }
}
